package fr.umlv.m2.android.quizz.singleplayer;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import fr.umlv.m2.android.quizz.HomepageActivity;
import fr.umlv.m2.android.quizz.R;
import fr.umlv.m2.android.quizz.database.Level;
import fr.umlv.m2.android.quizz.database.LevelDb;
import fr.umlv.m2.android.quizz.database.Score;
import fr.umlv.m2.android.quizz.database.ScoreDb;
import fr.umlv.m2.android.quizz.database.User;
import fr.umlv.m2.android.quizz.database.UserDb;

public class SinglePlayerResultActivity extends Activity {
	/**
	 * The result player text.
	 */
	private TextView playerText;
	/**
	 * The result number of good answer text.
	 */
	private TextView goodAnswerText;
	/**
	 * The result number of bad answer text.
	 */
	private TextView badAnswerText;
	/**
	 * The result percentage of success text.
	 */
	private TextView successPercentageText;
	/**
	 * The result time text.
	 */
	private TextView timeText;
	/**
	 * The back to home page button.
	 */
	private Button backToHomepage;
	
	/**
	 * The single player result intent.
	 */
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.single_player_result);
		
		this.playerText = (TextView)findViewById(R.id.singlePlayerResulPlayerText);
		this.goodAnswerText = (TextView)findViewById(R.id.singlePlayerResultGoodAnswerText);
		this.badAnswerText = (TextView)findViewById(R.id.singlePlayerResultBadAnswerText);
		this.successPercentageText = (TextView)findViewById(R.id.singlePlayerResultSuccessPercentageText);
		this.timeText = (TextView)findViewById(R.id.singlePlayerResultTimeText);
		this.backToHomepage = (Button)findViewById(R.id.singlePlayerResultButton);
		
		this.intent = this.getIntent();
		final Bundle bundle = this.intent.getExtras();
		final long userId = bundle.getLong("userId");
		final long levelId = bundle.getLong("levelId");
		final long themeId = bundle.getLong("themeId");
		
		LevelDb levelDb = new LevelDb(this);
		levelDb.open();
		Level level = levelDb.getLevelWithId(levelId);
		levelDb.close();
		
		final int goodAnswer = bundle.getInt("goodAnswer");
		final int badAnswer = bundle.getInt("badAnswer");
		final int successPercentage = Math.round((goodAnswer * 100) / level.getNbQuestion());
		final long longTime = bundle.getLong("time");
		Score currentScore = new Score(
				goodAnswer,
				badAnswer,
				successPercentage,
				levelId,
				userId,
				themeId
		);
		currentScore.setTime(longTime);
		
		UserDb userDb = new UserDb(this);
		userDb.open();
		User user = userDb.getUserWithId(userId);
		userDb.close();
		
		this.playerText.setText(user.getName());
		this.goodAnswerText.setText(String.valueOf(goodAnswer));
		this.badAnswerText.setText(String.valueOf(badAnswer));
		this.successPercentageText.setText(successPercentage + "%");
		Time time = new Time(longTime);
		this.timeText.setText(time.getMinutes() + ":" + time.getSeconds());
		
		ScoreDb scoreDb = new ScoreDb(this);
		scoreDb.open();
		
		final int nbScore = scoreDb.getNbScore(levelId, themeId);
		if(nbScore < 1) {
			scoreDb.insertScore(currentScore);
		} else {
			List<Score> scores = scoreDb.getSinglePlayerScores(levelId, themeId);
			List<Score> newScores = new ArrayList<Score>();
			for(Score score : scores) {
				if(score.getGoodAnswer() > currentScore.getGoodAnswer()) {
					newScores.add(score);
				} else if(score.getGoodAnswer() == currentScore.getGoodAnswer()) {
					if(score.getTime() <= currentScore.getTime()) {
						newScores.add(score);
					} else {
						newScores.add(currentScore);
						currentScore = score;
					}
				} else {
					newScores.add(currentScore);
					currentScore = score;
				}
			}
			
			for(Score score : newScores) {
				scoreDb.insertScore(score);
			}
		}
		
		scoreDb.close();
		
		this.backToHomepage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				intent = new Intent(SinglePlayerResultActivity.this, HomepageActivity.class);
				intent.putExtra("userId", userId);
				startActivity(intent);
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		return;
	}
}
